<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title th:if="${config.id}">编辑配置</title>
  <title th:unless="${config.id}">新增配置</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }
    .container { max-width: 800px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
    h1 { color: #333; }
    .form-group { margin-bottom: 15px; }
    label { display: block; margin-bottom: 5px; font-weight: bold; }
    input, select, textarea { width: 100%; padding: 8px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
    .btn { padding: 10px 20px; margin: 5px; background-color: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
    .btn:hover { background-color: #0056b3; }
    .btn-danger { background-color: #dc3545; }
    .btn-danger:hover { background-color: #c82333; }
    .rule-item { border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 4px; }
    .rule-actions { text-align: right; }
    .alert { padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px; }
    .alert-success { color: #155724; background-color: #d4edda; border-color: #c3e6cb; }
    .alert-danger { color: #721c24; background-color: #f8d7da; border-color: #f5c6cb; }
  </style>
</head>
<body>
<div class="container">
  <h1 th:if="${config.id}">编辑配置</h1>
  <h1 th:unless="${config.id}">新增配置</h1>

  <form th:object="${config}" method="post" th:action="@{/validation/config}">
    <input type="hidden" th:field="*{id}" />
    <div class="form-group">
      <label for="interfaceName">接口名称 *</label>
      <input type="text" id="interfaceName" th:field="*{interfaceName}" required />
    </div>
    <div class="form-group">
      <label for="description">描述</label>
      <textarea id="description" th:field="*{description}"></textarea>
    </div>
    <div class="form-group">
      <label>
        <input type="checkbox" th:field="*{enabled}" /> 启用
      </label>
    </div>

    <h3>校验规则</h3>
    <div id="rules-container">
      <div class="rule-item" th:each="rule, iterStat : *{rules}">
        <input type="hidden" th:name="'rules[' + ${iterStat.index} + '].id'" th:value="${rule.id}" />
        <div class="form-group">
          <label>字段名 *</label>
          <input type="text" th:name="'rules[' + ${iterStat.index} + '].fieldName'" th:value="${rule.fieldName}" required />
        </div>
        <div class="form-group">
          <label>规则类型 *</label>
          <select th:name="'rules[' + ${iterStat.index} + '].ruleType'" th:value="${rule.ruleType}" required>
            <option value="NOT_NULL">非空</option>
            <option value="NOT_BLANK">非空字符串</option>
            <option value="PATTERN">正则表达式</option>
            <option value="MIN_LENGTH">最小长度</option>
            <option value="MAX_LENGTH">最大长度</option>
            <option value="MIN_VALUE">最小值</option>
            <option value="MAX_VALUE">最大值</option>
            <option value="IN_LIST">列表校验</option>
            <option value="CUSTOM">自定义</option>
          </select>
        </div>
        <div class="form-group">
          <label>规则值</label>
          <input type="text" th:name="'rules[' + ${iterStat.index} + '].ruleValue'" th:value="${rule.ruleValue}" placeholder="根据规则类型填写" />
        </div>
        <div class="form-group">
          <label>提示信息</label>
          <input type="text" th:name="'rules[' + ${iterStat.index} + '].message'" th:value="${rule.message}" placeholder="可选" />
        </div>
        <div class="form-group">
          <label>排序序号</label>
          <input type="number" th:name="'rules[' + ${iterStat.index} + '].orderIndex'" th:value="${rule.orderIndex}" min="0" step="1" />
        </div>
        <div class="rule-actions">
          <button type="button" class="btn btn-danger" onclick="removeRule(this)">删除</button>
        </div>
      </div>
    </div>
    <button type="button" class="btn" onclick="addRule()">添加规则</button>

    <div class="form-group">
      <button type="submit" class="btn">保存</button>
      <a href="/validation/configs" class="btn">取消</a>
    </div>
  </form>

  <script>
    let ruleIndex = [[${#lists.size(config.rules)}]];

    function addRule() {
        const container = document.getElementById('rules-container');
        const newRuleHtml = `
            <div class="rule-item">
                <input type="hidden" name="rules[\${ruleIndex}].id" value="" />
                <div class="form-group">
                    <label>字段名 *</label>
                    <input type="text" name="rules[\${ruleIndex}].fieldName" required />
                </div>
                <div class="form-group">
                    <label>规则类型 *</label>
                    <select name="rules[\${ruleIndex}].ruleType" required>
                        <option value="NOT_NULL">非空</option>
                        <option value="NOT_BLANK">非空字符串</option>
                        <option value="PATTERN">正则表达式</option>
                        <option value="MIN_LENGTH">最小长度</option>
                        <option value="MAX_LENGTH">最大长度</option>
                        <option value="MIN_VALUE">最小值</option>
                        <option value="MAX_VALUE">最大值</option>
                        <option value="IN_LIST">列表校验</option>
                        <option value="CUSTOM">自定义</option>
                    </select>
                </div>
                <div class="form-group">
                    <label>规则值</label>
                    <input type="text" name="rules[\${ruleIndex}].ruleValue" placeholder="根据规则类型填写" />
                </div>
                <div class="form-group">
                    <label>提示信息</label>
                    <input type="text" name="rules[\${ruleIndex}].message" placeholder="可选" />
                </div>
                <div class="form-group">
                    <label>排序序号</label>
                    <input type="number" name="rules[\${ruleIndex}].orderIndex" min="0" step="1" />
                </div>
                <div class="rule-actions">
                    <button type="button" class="btn btn-danger" onclick="removeRule(this)">删除</button>
                </div>
            </div>`;
        container.insertAdjacentHTML('beforeend', newRuleHtml);
        ruleIndex++;
    }

    function removeRule(button) {
        button.closest('.rule-item').remove();
    }
  </script>
</div>
</body>
</html>